package end.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import end.bean.Article;
import end.ser.ArticleSER;
import end.util.JsonUtil;

/**
 * 
 * ArticleForController.java 2016-6-28
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Controller
@RequestMapping("/article/")
public class ArticleForController {
	private final static Logger log = LogManager
			.getLogger(ArticleForController.class.getName());

	@Autowired
	private ArticleSER<Article> articleSER;

	public ArticleSER<Article> getArticleSER() {
		return articleSER;
	}

	public void setArticleSER(ArticleSER<Article> articleSER) {
		this.articleSER = articleSER;
	}

	@RequestMapping("queryArticlesByPage")
	public @ResponseBody
	Map<String, Object> queryArticlesByPage(Integer page, Integer rows,
			String sort, String order) throws Exception {
		Article article = new Article();

		article.getPageUtil().setPaging(true);
		article.getPageUtil().setOrderByCondition(sort);
		article.getPageUtil().setQueryCondition(order);
		article.getPageUtil().setPageOffset((page - 1) * rows);
		article.getPageUtil().setPageSize(rows);
		article.setUseStatus('0');

		List<Article> beans = articleSER.selectByBean(article);
		Map<String, Object> rets = new HashMap<String, Object>();
		rets.put("total", articleSER.selectByBeanCount(article));
		rets.put("rows", beans);
		return rets;
	}

	@RequestMapping("drop")
	public @ResponseBody
	boolean drop(int[] drops) throws Exception {
		List<Integer> dropsArray = new ArrayList<Integer>();
		for (Integer integer : drops) {
			dropsArray.add(integer);
		}
		articleSER.deleteByPrimaryKey(dropsArray);
		return true;
	}

	@RequestMapping("add")
	public @ResponseBody
	boolean insertArticle(Article article, MultipartFile displayPic,
			HttpSession session) throws Exception {
		article.setUseStatus('0');
		article.setWriteTime(new Date());
		if (log.isDebugEnabled()) {
			log.debug("article {} 被添加 ：" + JsonUtil.toJson(article));
		}
		articleSER.insert(article);
		if (!displayPic.isEmpty()) {
			ServletContext context = session.getServletContext();
			String realPath = context.getRealPath("/static/pic/article_pic");
			String suffix = displayPic.getOriginalFilename().substring(
					displayPic.getOriginalFilename().lastIndexOf("."));
			displayPic.transferTo(new File(realPath + "/article" + "_"
					+ article.getId() + suffix));
			article.setPicture("article_" + article.getId() + suffix);
			articleSER.updateByPrimaryKey(article);
		}
		return true;
	}

	@RequestMapping("modify")
	public @ResponseBody
	boolean modifyArticle(Article article, MultipartFile displayPic,
			HttpSession session) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug(JsonUtil.toJson(article) + "    被添加");
		}
		if (!displayPic.isEmpty()) {
			ServletContext context = session.getServletContext();
			String realPath = context.getRealPath("/static/pic/article_pic");
			FileUtil.deleteContents(new File(realPath + "/"
					+ article.getPicture()));
			String suffix = displayPic.getOriginalFilename().substring(
					displayPic.getOriginalFilename().lastIndexOf("."));
			displayPic.transferTo(new File(realPath + "/article" + "_"
					+ article.getId() + suffix));
			article.setPicture("article_" + article.getId() + suffix);
		}
		articleSER.updateByPrimaryKey(article);
		return true;
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
	}
}
